var vm = new Vue({
    el: '#contentContainer',
    data: {
        form: {
            processDefinitionId: '',
            processDefinitionName: '',
            processCategoryId: null,
            processNodes: 0,
            taskNodes: []
        },
        rules: {
            processDefinitionName: [
                {required: true, message: '请输入流程名称', trigger: 'blur'},
                {min: 0, max: 64, message: '长度不能超过64个字符', trigger: 'blur'}
            ],
            processCategoryId: [
                {required: true, message: '请选择流程分类', trigger: 'change'}
            ],
            processNodes: [
                {required: true, message: '请输入流程节点数', trigger: 'blur'}
            ],
            taskName: [
                {required: true, message: '请输入节点名称', trigger: 'blur'},
                {min: 0, max: 64, message: '长度不能超过64个字符', trigger: 'blur'}
            ],
            candidate: [
                {type: 'array', required: true, message: '请选择候选人', trigger: 'change'}
            ]
        },
        processCategoryList: [],
        maxTaskNodeCount: 15,
        userList: [],
        searching: false
    },
    created: function () {
        var $this = this;

        Common.getJson('/process/category/getAllProcessCategoryList', {},
            function (response) {
                if (!response || response.status != 200)
                    return;

                $this.processCategoryList = response.result;

                Common.getJson('/process/definition/getProcessDefinitionInfo',
                    {processDefinitionId: Common.getUrlParam('processDefinitionId')},
                    function (response) {
                        if (!response || response.status != 200)
                            return;

                        var processDefinition = response.result.processDefinition;
                        $this.form.processDefinitionId = processDefinition.processDefinitionId;
                        $this.form.processDefinitionName = processDefinition.processDefinitionName;
                        $this.form.processCategoryId = processDefinition.processCategoryId == 0 ? null : processDefinition.processCategoryId;
                        $this.form.processNodes = processDefinition.processNodes;
                        $this.userList = response.result.userList;
                        $this.form.taskNodes = response.result.taskNodes;
                    });
            });
    },
    methods: {
        submitForm: function (formName) {
            var $this = this;

            $this.$refs[formName].validate(function (valid) {
                if (!valid)
                    return false;

                Common.postJson('/process/definition/edit', $this.form,
                    function (response) {
                        if (!response)
                            return;

                        //保存成功
                        if (response.status == 200 && response.result.success)
                            window.location.href = '/process/definition/list';
                        else {
                            var errMsg = response.status == 200 ? response.result.errMsg : response.msg;

                            $this.$notify.error({
                                title: '错误',
                                message: errMsg,
                                offset: $("#main", window.parent.document).height() - 120,
                                duration: 0
                            });
                        }
                    });
            });
        },
        resetForm: function (formName) {
            this.$refs[formName].resetFields();
        },
        onSearchUser: function (userName) {
            var $this = this;

            if (userName) {
                $this.searching = true;

                Common.getJson('/sys/user/getUserList',
                    {
                        pageNum: 1, pageSize: 10, userName: userName
                    },
                    function (response) {
                        if (!response || response.status != 200)
                            return;

                        var result = response.result;
                        $this.userList = result.list;
                        $this.searching = false;
                    });
            }
        },
        onCandidateChange: function () {
            this.userList = [];
        },
        onProcessNodeCountChange: function (val) {
            this.form.taskNodes = [];
            for (var i = 1; i <= val; i++) {
                this.form.taskNodes.push({
                    taskName: '',
                    candidate: []
                });
            }
        }
    }
});